﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EnterpriseFund.Common;
using DevExpress.Web.Mvc;
using System.Data;

namespace EnterpriseFund.Controllers
{
        [OutputCache(Duration = 0, VaryByParam = "none")]
    public class AllowanceApplicationFormcheckController : AdminBaseController
    {
        //
        // GET: /AllowanceApplicationFormAudit/
            [AuthorityActionFilter("m_AllowanceApplicationFormcheck", Order = 1)]
            [LogPageActionFilter("申请复核", Order = 2)]
        public ActionResult Index()
        {
            string strwhere = " FStatus='2' and FIsReturn='N' ";
            ViewData["FStatus"] = "2";
            List<Model.AllowanceApplicationForm> AllowanceApplicationFormList = new List<Model.AllowanceApplicationForm>();
            AllowanceApplicationFormList = GetAllowanceApplicationFormListList(strwhere, 1);
            return View(AllowanceApplicationFormList);
        }

        public ActionResult AllowanceApplicationFormcheckEdit(string FApplicationFormID)
        {
            BLL.AllowanceApplicationForm allowanceApplicationFormBLL = new BLL.AllowanceApplicationForm();
            Model.AllowanceApplicationForm model = allowanceApplicationFormBLL.GetModel(FApplicationFormID);
            model.FApplyAmountDX = Common.Rmb.CmycurD(model.FApplyAmount);
            model.FAuditAmountDX = Common.Rmb.CmycurD(model.FAuditAmount);
            BLL.DJ_NSRXX DJ_NSRXXBLL = new BLL.DJ_NSRXX();
            return View("AllowanceApplicationFormcheckEdit", model);
        }

        public ActionResult AllowanceApplicationFormcheckListPartial()
        {

            int pageIndex = Request.Params["pageIndex"] == null ? 0 : int.Parse(Request.Params["pageIndex"].ToString());
            int pageCount = Request.Params["PageCount"] == null ? 0 : int.Parse(Request.Params["PageCount"].ToString());
            int pageRowSize = Request.Params["pageRowSize"] == null ? 0 : int.Parse(Request.Params["pageRowSize"].ToString());
            int pageRowCount = Request.Params["pageRowCount"] == null ? 0 : int.Parse(Request.Params["pageRowCount"].ToString());
            string callbackArg = DevExpressHelper.CallbackArgument;

            string command = Request.Params["command"] == null ? "" : Request.Params["command"].ToString();

            if (command == "PAGERONCLICK")
            {
                string[] args = callbackArg.Split(new char[1] { '|' });
                string arg = args[args.Count() - 1];

                switch (arg)
                {
                    case "PBN;":
                        {
                            //下一页
                            pageIndex = pageIndex + 2;
                            break;
                        }
                    case "PBP;":
                        {
                            //上一页
                            //pageIndex = pageIndex;
                            break;
                        }
                    case "PBL;":
                        {
                            //最后一页
                            pageIndex = pageCount;
                            break;
                        }
                    case "PBF;":
                        {
                            //第一页
                            pageIndex = 1;
                            break;
                        }
                    default:
                        {
                            //点击数字页码
                            string tempIndex = arg.Replace("PN", "");
                            tempIndex = tempIndex.Replace(";", "");
                            if (RegExp.IsNumeric(tempIndex))
                            {
                                pageIndex = int.Parse(tempIndex) + 1;
                            }
                            break;
                        }

                }
            }
            else
            {
                pageIndex = pageIndex + 1;
            }
            string strwhere = "1=1 ";
            string FStatus = Request.Params["FStatus"];
            string NSRBM = Request.Params["NSRBMQ"];
            if (!string.IsNullOrEmpty(FStatus))
            {
                if (FStatus == "2")
                {
                    strwhere += "and FStatus='2' and  FIsReturn='N'";
                }
                else
                {
                    strwhere += "  and FStatus='" + FStatus + "'";
                }
            }
            else
            {
                strwhere += "  and  FStatus='2' and FIsReturn='N' ";
            }

            if (!string.IsNullOrEmpty(NSRBM))
            {
                strwhere += " and  NSRMC like '%" + NSRBM + "%' ";
            }
           

            List<Model.AllowanceApplicationForm> AllowanceApplicationFormList = new List<Model.AllowanceApplicationForm>();
            AllowanceApplicationFormList = GetAllowanceApplicationFormListList(strwhere, pageIndex);
            return PartialView("AllowanceApplicationFormcheckListPartial", AllowanceApplicationFormList);
        }

        /// <summary>
        /// 获取定义的工作时间数据
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <param name="pageIndex">第几页</param>
        /// <returns>查询出来的数据</returns>
        private List<Model.AllowanceApplicationForm> GetAllowanceApplicationFormListList(string strWhere, int pageIndex)
        {
            BLL.Common commonBll = new BLL.Common();
            BLL.AllowanceApplicationForm AllowanceApplicationFormBLL = new BLL.AllowanceApplicationForm();
            string fiedls = "FApplicationFormID";
            int total;
            DataSet ds = commonBll.GetListByPage("fund.t_AllowanceApplicationForm", fiedls, Common.GlobalConstant.CONSTRING_PAGESIZE, pageIndex, strWhere, "FApplicationFormID", out total);
            List<Model.AllowanceApplicationForm> AllowanceApplicationFormList = AllowanceApplicationFormBLL.DataTableToList(ds.Tables[0]);
            //foreach (Model.AllowanceApplicationForm a in AllowanceApplicationFormList)
            //{
            //    a.FApplyAmount = a.FApplyAmount / 10000;
            //    a.FAuditAmount = a.FAuditAmount / 10000;
            //}
            List<Model.AllowanceApplicationForm> AllowanceApplicationFormAllList = new List<Model.AllowanceApplicationForm>();
            total = AllowanceApplicationFormBLL.GetRecordCount(strWhere);
            int pageRowSize = Common.GlobalConstant.CONSTRING_PAGESIZE;
            for (int i = 0; i < total; i++)
            {
                AllowanceApplicationFormAllList.Add(null);
            }
            for (int i = 0; i < pageRowSize; i++)
            {
                if (AllowanceApplicationFormList.Count > i)
                {
                    AllowanceApplicationFormAllList[((pageIndex - 1) * pageRowSize) + i] = AllowanceApplicationFormList[i];
                }
            }

            return AllowanceApplicationFormAllList;
        }
        public string SaveAll(string nolist)
        {
            string[] listFApplicationFormID = nolist.Split(',');
            foreach (string FApplicationFormID in listFApplicationFormID)
            {
                try
                {
                    if (FApplicationFormID != null & FApplicationFormID != "")
                    {
                        Model.AllowanceApplicationForm model = new Model.AllowanceApplicationForm();
                        model.FApplicationFormID = FApplicationFormID;
                        //   model.FApplyName = "";

                        model.FStatus = "3";
                        model.FIsReturn = "N";



                        BLL.AllowanceApplicationForm allowanceApplicationFormBLL = new BLL.AllowanceApplicationForm();
                        allowanceApplicationFormBLL.UpdateFStatus3(model);

                        WorkFlow("t_AllowanceApplicationForm", "30", FApplicationFormID, "open", "");

                        ViewData["jsAlert"] = "操作成功";
                    }
                }
                catch (Exception ex)
                {
                    return ex.Message;
                    //  ViewData["jsAlert"] = ex.Message;
                }

            }
            return "ok";
        }
        public ActionResult Save()
        {
            string FApplicationFormID = Fetch.Post("hidFApplicationFormID");
            try
            {
                Model.AllowanceApplicationForm model = new Model.AllowanceApplicationForm();
                model.FApplicationFormID = FApplicationFormID;
                string type = Fetch.Post("hidFStatus");
                switch (type)
                {
                    case "open":
                        model.FStatus = "3";
                        model.FIsReturn = "N";
                        break;
                    case "Return":
                        model.FStatus = "2";
                        model.FIsReturn = "Y";
                        break;
                }

                if (type == "open")
                {
                    WorkFlow("t_AllowanceApplicationForm", "30", FApplicationFormID, "open", "");
                }

                BLL.AllowanceApplicationForm allowanceApplicationFormBLL = new BLL.AllowanceApplicationForm();
                allowanceApplicationFormBLL.UpdateFStatus2(model);
                ViewData["jsAlert"] = "操作成功";
            }
            catch (Exception ex)
            {
                ViewData["jsAlert"] = ex.Message;
            }

            return AllowanceApplicationFormcheckEdit(FApplicationFormID);
        }



    }
}
